package com.zqxq.lottery.util;

import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtil extends DateUtils{



    /**
     * 说    明：   根据当前日期获取下次开奖时间--双色球
     * 创 建 人：	一刀·胡
     * 创建时间：	2018/3/26 11:41
     * 修 改 人：
     * 修改日期：
     **/
    public static Date getNextOpenDateSSQ(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        if (weekDay >= Calendar.SUNDAY && weekDay < Calendar.TUESDAY)
            date = getNextDayOfWeek(cal, Calendar.TUESDAY);
        else if (weekDay >= Calendar.TUESDAY && weekDay < Calendar.THURSDAY)
            date = getNextDayOfWeek(cal, Calendar.THURSDAY);
        else if (weekDay >= Calendar.THURSDAY)
            date = getNextDayOfWeek(cal, Calendar.SUNDAY);
        return date;
    }

    /**
     * 说    明：   根据当前日期获取下次开奖时间--双色球
     * 创 建 人：	一刀·胡
     * 创建时间：	2018/3/26 11:51
     * 修 改 人：
     * 修改日期：
     **/
    public static Date getNextOpenDateDLT(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        if (weekDay == Calendar.SATURDAY ){
            cal.add(Calendar.DATE, 7);
            date = getNextDayOfWeek(cal,Calendar.MONDAY);
        }
        else if (weekDay < Calendar.MONDAY)
            date = getNextDayOfWeek(cal, Calendar.MONDAY);
        else if (weekDay >= Calendar.MONDAY && weekDay < Calendar.WEDNESDAY)
            date = getNextDayOfWeek(cal, Calendar.WEDNESDAY);
        else if (weekDay >= Calendar.WEDNESDAY)
            date = getNextDayOfWeek(cal, Calendar.SATURDAY);
        return date;
    }

    /**
     * 说    明：   根据当前日期获取下次开奖时间--福彩、排列（每天开奖）
     * 创 建 人：	一刀·胡
     * 创建时间：	2018/3/26 11:51
     * 修 改 人：
     * 修改日期：
     **/
    public static Date getNextOpenDateFCAndPL(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        if (weekDay == Calendar.SATURDAY ){
            date = getNextDayOfWeek(cal,Calendar.SUNDAY);
        }else {
            date = DateUtils.addDay(date,1);
        }
        return date;
    }

    /**
     * 说    明：   根据当前日期获取下次开奖时间--七星彩 每周二、五、日 20:30开奖
     * 创 建 人：	一刀·胡
     * 创建时间：	2018/3/26 11:51
     * 修 改 人：
     * 修改日期：
     **/
    public static Date getNextOpenDateQXC(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        if (weekDay >= Calendar.SUNDAY && weekDay < Calendar.TUESDAY){
            date = getNextDayOfWeek(cal,Calendar.TUESDAY);
        }
        else if (weekDay >= Calendar.TUESDAY && weekDay < Calendar.FRIDAY)
            date = getNextDayOfWeek(cal,Calendar.FRIDAY);

        else if (weekDay >= Calendar.FRIDAY)
            date = getNextDayOfWeek(cal,Calendar.SUNDAY);
        return date;
    }

    /**
     * 说    明：   获取某周的星期几日期
     * 创 建 人：	一刀·胡
     * 创建时间：	2018/3/26 11:40
     * 修 改 人：
     * 修改日期：
     **/
    public static Date getNextDayOfWeek(Calendar calendar, int day) {
        if (Calendar.SUNDAY == day) {
            calendar.add(Calendar.DATE, 7);
        }
        calendar.set(Calendar.DAY_OF_WEEK, day);
        return calendar.getTime();
    }

    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    public static void main(String[] args) {
        Date date = new Date();
        date.setDate(21);
        for(int i= 0;i<10;i++){
            date = getNextOpenDateDLT(date);
            log.info(DateUtils.dateToStr(date));
        }
        log.info("----------------------------------------");
        Date date2 = new Date();
        date2.setDate(21);
        for(int i= 0;i<10;i++){
            date2 = getNextOpenDateSSQ(date2);
            log.info(DateUtils.dateToStr(date2));
        }
        log.info("----------------------------------------");
        System.out.println(DateUtils.dateToStr(getNextOpenDateFCAndPL(DateUtils.addDay(new Date(),1))));
        log.info("----------------------------------------");
        System.out.println(DateUtils.dateToStr(getNextOpenDateQXC(new Date())));
    }

    /**
    * @Author 苦智
    * @Description
    * @Date 11:09 2018/8/18
    * @Param [strDate, type] type:1数字日期 2：日期
    * @return java.lang.String
    **/
    public static String getWeek(String strDate,Integer type) {
        String[] weekIndex = {"7","1","2","3","4","5","6"};
        String[] week = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Date date = new Date();
        if (StrUtils.isNotEmpty(strDate)){
            date = DateUtils.strToDate(strDate,"yyyy-MM-dd HH:mm:ss");
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (i<0){
            i = 0;
        }
        if (type == 1){
            return weekIndex[i];
        }else {
            return week[i];
        }

    }
}
